<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog"
             title="课程信息"
             append-to-body width="44%" top="5vh">
    <div class="head-container">
      <el-input v-model="name" clearable placeholder="输入名称搜索" maxlength="10" size="medium" style="width: 250px;"
                class="filter-item" @keyup.enter.native="init"/>
      <treeselect  class="filter-item treeselect-main" v-model="typeId"  :options="courseType" style="width: 250px" placeholder="选择课程分类"/>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="init">搜索</el-button>
    </div>
    <el-table ref="table" v-loading="loading" :data="data" size="small" style="width: 100%;" border>
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="typeName" label="课程分类" width="100"/>
      <el-table-column prop="courseName" label="课程名称"/>
    </el-table>
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="okSelect">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import initData from '@/mixins/initData'
  import { typeTree } from '@/api/business/courseType'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  export default {
    name: 'courseSelect',
    mixins: [initData],
    components: { Treeselect},
    data() {
      return {
        dialog: false,
        name: '',
        student: [],
        typeId:null,
        courseType: []
      }
    },
    methods: {
      cancel() {
        this.dialog = false
      },
      beforeInit() {
        this.url = 'course/page-list'
        this.params = { page: this.page, size: this.size, courseName: this.name,typeId:this.typeId }
        this.getCourseType();
        return true
      },
      getCourseType() {
        typeTree().then(data => {
          this.courseType = data
        })
      },
      okSelect() {
        const data = this.$refs.table.selection
        const courseInfo = []
        for (let i = 0; i < data.length; i++) {
          const user = { courseId: data[i].id, courseName: data[i].courseName }
          courseInfo.push(user)
        }
        this.$emit('setCourseInfo', courseInfo)
        this.cancel()
      }
    }
  }
</script>

<style scoped>
  .treeselect-main {
      margin-top: 1px;
      line-height: 34px;
      width: 200px;
      .vue-treeselect__placeholder {
        line-height: 34px;
      }
      .vue-treeselect__control {
        height: 35px;
      }
    }
</style>
